<template>
  <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="30%">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleFormRef"
      label-width="100px"
    >
      <el-form-item label="分类名称:" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="备注:" prop="remark">
        <el-input type="textarea" v-model="ruleForm.remark"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    // RuleForm: Object,
    dialogVisible: {
      type: Boolean,
      default: () => {
        return false;
      }
    },
    dialogTitle: {
      type: String,
      default: () => {
        return "新增";
      }
    }
  },
  data() {
    return {
      ruleForm: {
        name: "",
        remark: "",
      },
      rules: {
        name: [
          { required: true, message: "角色名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  methods: {
    // 新增 编辑 提交
    submit() {
      this.$refs.ruleFormRef.validate(valid => {
        if (valid) {
          if (this.dialogTitle == "新增") {
            this.$emit("addItem", this.ruleForm);
          } else {
            this.$emit("editItem", this.ruleForm);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 打开dialog时清空表单信息
    beforeResetForm() {
      this.$nextTick(() => {
        this.$refs.ruleFormRef.resetFields();
      });
    },
    // 编辑后表单信息
    changeRuleForm(e) {
      console.log(e);
      this.ruleForm = e;
    }
  }
};
</script>

<style scoped></style>
